package com.shaoshaohuo.app.ui.ec;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.BusinessBuyer;
import com.shaoshaohuo.app.entity.Buyer;
import com.shaoshaohuo.app.entity.MarketDetailEntity;
import com.shaoshaohuo.app.entity.PurchaseDetail.ProductMore;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.adapter.MerchantsDetailListAdapter;
import com.shaoshaohuo.app.adapter.ProductGridviewAdapter;
import com.shaoshaohuo.app.view.MeasureGridView;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.utils.ImageLoadUtil;

public class MarketDetailActivity extends BaseActivity {

    private TopbarView mTopbarView;

    private ImageView mLogoImage;

    private TextView mTitleText;

    private TextView mYearTotalText;

    private TextView mAddressText;

    private MeasureGridView mProductGv;

    private XListView mListView;

    private String marketid = "";

    private String buyersize = "50";

    private String marketName = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_market_detail);
        initData();
        initView();
        setUpView();
        requestData();
    }

    private void initData() {
        Intent intent = getIntent();
        marketid = intent.getStringExtra(ExtraName.id);
        marketName = intent.getStringExtra(ExtraName.marketName);
    }

    private void requestData() {
        startLoadingDialog();
        RequestService.getInstance().getMarketDetail(this, marketid, buyersize, MarketDetailEntity.class, new RequestListener() {

            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    MarketDetailEntity entity = (MarketDetailEntity) resultData;
                    updateUi(entity);
                } else {
                    showToast(resultData.getMsg());
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
            }
        });
    }

    protected void updateUi(MarketDetailEntity entity) {
        BusinessBuyer data = entity.getData();
        mTitleText.setText(data.getMarketname());
        mYearTotalText.setText("全年采购金额：" + data.getYearmoney() + "w");
        mAddressText.setText(data.getAddress());
        ImageLoader.getInstance().displayImage(data.getLogo(), mLogoImage, ImageLoadUtil.getDisplayImageOptions(R.drawable.market_default));
        ProductGridviewAdapter<ProductMore> adapter1 = new ProductGridviewAdapter<>(this, data.getCategorys(), false);
        mProductGv.setAdapter(adapter1);
        MerchantsDetailListAdapter<Buyer> adapter2 = new MerchantsDetailListAdapter<>(this, data.getBuyers(), false);
        mListView.setAdapter(adapter2);
    }

    private void initView() {
        mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
        mTitleText = (TextView) this.findViewById(R.id.textview_title);
        mYearTotalText = (TextView) this.findViewById(R.id.textview_caigou_total);
        mAddressText = (TextView) this.findViewById(R.id.textview_address);
        mProductGv = (MeasureGridView) this.findViewById(R.id.gv_service_list);
        mListView = (XListView) this.findViewById(R.id.listview_data_list);
        mLogoImage = (ImageView) this.findViewById(R.id.imageview_image);
    }

    private void setUpView() {
        mTopbarView.setCenterText(marketName);
        mTopbarView.setLeftView(true, true);
        mListView.setPullLoadEnable(false);
        mListView.setPullRefreshEnable(false);
        // List<String> list = new ArrayList<>();
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        // ProductGridviewAdapter<String> adapter = new ProductGridviewAdapter<>(this, list, false);
        // mProductGv.setAdapter(adapter);
        //
        // MerchantsDetailListAdapter<String> adapter1 = new MerchantsDetailListAdapter<>(this,
        // list, false);
        // mListView.setAdapter(adapter1);
    }
}
